package com.zyy.smartcity1129.zhdj;

import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.DYLearnInfo;
import com.zyy.smartcity1129.data.PLInfo;
import com.zyy.smartcity1129.home.HomeFragment;
import com.zyy.smartcity1129.util.RvAd;

import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName: DYLearn1Fragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 12/12/2021$
 * @Version: 1.0
 */
public class DYLearn1Fragment extends BaseFragment {
    private DYLearnInfo dyLearnInfos = DYLearnFragment.dyLearnInfos;
    private ImageView imgMovieContext1;
    private TextView tvMovieContext1;
    private LinearLayout lineMovieContext2;
    private VideoView videoViewMovieContext;
    private LinearLayout lineMoveContext3;
    private TextView tvMovieContext21;
    private LinearLayout lineMoveContext5;
    private EditText edit;
    private EditText edit10;
    private Button btn;
    private RecyclerView rvMovieContext;

    @Override
    protected int layId() {
        return R.layout.f_dyxx1;
    }

    @Override
    protected String title() {
        return dyLearnInfos.getName();
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return false;
    }

    RvAd<PLInfo> rvAd;
    LinkedList<PLInfo> list = new LinkedList<>();

    @Override
    protected void init(View iv) {
        initView(iv);
        Glide.with(imgMovieContext1.getContext()).load(dyLearnInfos.getImg()).into(imgMovieContext1);
        tvMovieContext1.setText(dyLearnInfos.getName());
//        if (dyLearnInfos.getModel() == 0) {
            videoViewMovieContext.setVideoURI(Uri.parse("android.resource://"+ getContext().getPackageName()+"/" + dyLearnInfos.getRaw()));
            MediaController mediaController = new MediaController(getContext());
            mediaController.setMediaPlayer(videoViewMovieContext);
            videoViewMovieContext.setMediaController(mediaController);
            videoViewMovieContext.requestFocus();
            videoViewMovieContext.start();
//        } else {
//            videoViewMovieContext.setVideoURI(Uri.parse("android.resource://"+ getContext().getPackageName()+"/" + dyLearnInfos.getRaw()));
//            MediaController mediaController = new MediaController(getContext());
//            mediaController.setMediaPlayer(videoViewMovieContext);
//            videoViewMovieContext.setMediaController(mediaController);
//            videoViewMovieContext.requestFocus();
//            videoViewMovieContext.start();
//        }
        rvMovieContext.setLayoutManager(new LinearLayoutManager(getContext()));
        initData();
        initAdapter(list);
        rvMovieContext.setAdapter(rvAd);
        tvMovieContext21.setOnClickListener(view -> {
            if (lineMoveContext5.getVisibility() == View.GONE) {
                lineMoveContext5.setVisibility(View.VISIBLE);
            } else {
                lineMoveContext5.setVisibility(View.GONE);
            }
        });
        btn.setOnClickListener(view -> {
            String sEdit = edit.getText().toString().trim();
            if (!sEdit.equals("")) {
                list.addFirst(new PLInfo(HomeFragment.userBeans.getNickName(), sEdit));
                initAdapter(list);
                rvMovieContext.setAdapter(rvAd);
                edit.setText("");
                Toast.makeText(getContext(), "发送成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getContext(), "内容不可为空", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initData() {
        list.add(new PLInfo("iSongZY", "学起来！！！"));
        list.add(new PLInfo("蜗得天", "以史为鉴，开创未来！"));
        list.add(new PLInfo("蜜汁跳舞", "马上记下[拳头][拳头][拳头][拳头]"));
        list.add(new PLInfo("柳州政法", "多么幸运我们拥有智慧，果敢，与稳重的掌舵人与先锋们 – 机不可失时不再来。感激，自豪有自强不息的同胞，谦逊、和平，勤奋劳作的每一个中国劳动者，生产者，创造者。也感激这精彩纷呈的广阔世界–你精彩了中国，中国丰富了你。"));
    }

    private void initAdapter(List<PLInfo> list) {
        rvAd = new RvAd<PLInfo>(list, R.layout.rv_pl) {
            private void initView(View vi) {
                tvNewsPL1 = (TextView) vi.findViewById(R.id.tvNewsPL1);
                tvNewsPL2 = (TextView) vi.findViewById(R.id.tvNewsPL2);
            }

            private TextView tvNewsPL2;
            private TextView tvNewsPL1;

            @Override
            protected void initR(View vi, PLInfo plInfo, int p) {
                initView(vi);
                tvNewsPL1.setText(plInfo.getName());
                tvNewsPL2.setText(plInfo.getContext());
            }
        };
    }

    private void initView(View iv) {
        imgMovieContext1 = (ImageView) iv.findViewById(R.id.imgMovieContext1);
        tvMovieContext1 = (TextView) iv.findViewById(R.id.tvMovieContext1);
        lineMovieContext2 = (LinearLayout) iv.findViewById(R.id.lineMovieContext2);
        videoViewMovieContext = (VideoView) iv.findViewById(R.id.videoViewMovieContext);
        lineMoveContext3 = (LinearLayout) iv.findViewById(R.id.lineMoveContext3);
        tvMovieContext21 = (TextView) iv.findViewById(R.id.tvMovieContext21);
        lineMoveContext5 = (LinearLayout) iv.findViewById(R.id.lineMoveContext5);
        edit = (EditText) iv.findViewById(R.id.edit);
        edit10 = (EditText) iv.findViewById(R.id.edit10);
        btn = (Button) iv.findViewById(R.id.btn);
        rvMovieContext = (RecyclerView) iv.findViewById(R.id.rvMovieContext);
    }
}
